package com.huxiu;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huxiu.base.ZiXunDetailPager;
import com.huxiu.lib.PagerScreenTab;
import com.huxiu.pagers.mycomment.MyComment;
import com.huxiu.pagers.mycomment.RemarkComment;
import com.huxiu.utils.Constants;
import com.huxiu.utils.LogUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;


public class MyCommentActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.indicator)
    private PagerScreenTab indicator;
    @ViewInject(R.id.pager)
    private ViewPager viewpager;

    private MyCommentActivity instance;
    private String aid;

    private boolean isMyComment=false;
    private boolean isRemarkComment=false;

    @ViewInject(R.id.layout_back)
    private RelativeLayout layout_back;

    @ViewInject(R.id.my_comment_title)
    private TextView my_comment_title;

    private MyAdapter myAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycomment);
        instance = this;
        ViewUtils.inject(this);
        if (!TextUtils.isEmpty(getIntent().getStringExtra("my_comment_title"))) {
            my_comment_title.setText("我的评论");
        }
        if (getIntent().hasExtra(Constants.ARTICLE_ID_KEY)) {
            aid = getIntent().getStringExtra(Constants.ARTICLE_ID_KEY);
        }
        broadcase();
        myAdapter=new MyAdapter();
        layout_back.setOnClickListener(this);
        viewpager.setAdapter(myAdapter);
        // 让指针控件绑定ViewPager
        indicator.setViewPager(viewpager);

    }

    String[] s = {"评论", "点评"};

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_back:
                instance.finish();
                break;
        }

    }


    public void broadcase(){
        IntentFilter intent = new IntentFilter();
        intent.addAction(Constants.INTENT_MYCOMMENT_NUM);
        intent.addAction(Constants.INTENT_REMARKCOMMENT_NUM);
        LocalBroadcastManager.getInstance(APP.getInstance()).registerReceiver(mReceiver, intent);
    }

    class MyAdapter extends PagerAdapter {
        // 返回标题
        @Override
        public CharSequence getPageTitle(int position) {
            return s[position];
        }

        @Override
        public int getCount() {
            return s.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        private ZiXunDetailPager pager;

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            switch (position) {
                case 0:
                    pager = new MyComment(instance);
                    container.addView(pager.rootView);
                    // 更新数据
                    pager.initData();
                    break;
                case 1:
                    pager = new RemarkComment(instance);
                    container.addView(pager.rootView);
                    // 更新数据
                    pager.initData();
                    break;
            }
            return pager.rootView;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
    //广播接收者
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, final Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constants.INTENT_MYCOMMENT_NUM)) {
                    int mycommentmun=intent.getIntExtra(Constants.COMMENT_NUM,0);
                s[0]="评论"+"（"+mycommentmun+"）";
                if(!isMyComment){
                    isMyComment=true;
                    myAdapter.notifyDataSetChanged();
                    indicator.notifyDataSetChanged();
                }
            }else if(action.equals(Constants.INTENT_REMARKCOMMENT_NUM)){
                int mycommentmun=intent.getIntExtra(Constants.COMMENT_NUM,0);
                s[1]="点评"+"（"+mycommentmun+"）";
                if(!isRemarkComment){
                    isRemarkComment=true;
                    myAdapter.notifyDataSetChanged();
                    indicator.notifyDataSetChanged();
                }
            }
            Log.e("aaaa","s[0]"+s[0].toString());
            Log.e("aaaa", "s[1]" + s[1].toString());
        }
    };



    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }


}
